package com.example.blog.controller;

import com.example.blog.entity.Article;
import com.example.blog.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TagCtl {

    @Autowired
    private ArticleRepository articleRepository;

    @GetMapping("/tagName")
    public String listArticlesByTag(@RequestParam("tagName") String tagName,
                                    @RequestParam(name = "page", defaultValue = "0") int page,
                                    Model model) {
        int size = 4;
        Pageable pageable = PageRequest.of(page, size);// 假设每页显示10篇文章，页码为0
        Page<Article> articles = articleRepository.findByTagName(tagName, pageable);
        model.addAttribute("articles", articles.getContent());
        model.addAttribute("currentPage", articles.getNumber());
        model.addAttribute("totalPages", articles.getTotalPages());
        model.addAttribute("totalElements", articles.getTotalElements());
        model.addAttribute("tagName",tagName);
        return "tagarticle"; // 返回的文章列表视图名称，你需要创建这个视图
    }
}